package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {
  @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest httpServletRequest,@RequestBody Employee employee){
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee>lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(lqw);
        if(one==null){
            return R.error("用户名错误");
        }else if(!one.getPassword().equals(password)){
            return R.error("密码错误");
        }else if(one.getStatus()==0){
            R.error("账号已禁用");
        }

        httpServletRequest.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }
    @PostMapping("/logout")
    public R<String>logout(HttpServletRequest httpServletRequest){
        httpServletRequest.getSession().removeAttribute("employee");
        return R.success("登出成功");
    }
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){
       // employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        Long id =(Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(id);
        //employee.setUpdateUser(id);
        employeeService.save(employee);
        return R.success("保存成功");
    }
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name ){
        Page page1=new Page(page,pageSize);
        LambdaQueryWrapper<Employee>lqw=new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name)&&!name.equals("null"),Employee::getName,name);
        employeeService.page(page1,lqw);
        log.info("page1=========="+page1);
        return R.success(page1);
    }
    @PutMapping
    public R<String>update(@RequestBody Employee employee,HttpServletRequest request){
        log.info(employee.toString());
        Long empid = (Long) request.getSession().getAttribute("employee");
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(empid);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee>getByid(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee!=null){return R.success(employee);}
        return R.error("获取失败");
    }

}
